package com.sduept.nwld.dataserver.manager.etl;

import com.sduept.bigdata.fault.common.ETLDAO;
import com.sduept.bigdata.fault.protection.model.ProtectChannelAlarm;
import com.sduept.bigdata.fault.protection.service.ProtectChannelAlarmPersistManager;
import com.sduept.core.dao.SuperDAO;
import com.sduept.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ProtectChannelAlarmETLManager {
	@Autowired
	private ETLDAO eDao;
	
	@Autowired
	private SuperDAO dao;
	
	@Autowired
	private ProtectChannelAlarmPersistManager manager; 
	
	public  void receiveAndSaveAllData() {
		List<?> list = getAllSourceData();
		receiveAndSave(list);
	}
	public  void receiveAndSavePartData(String date) {
		List<?> list = getAllSourceData(date);
		receiveAndSave(list);
	}
	
	
	public List<?> getAllSourceData(){
		List <?>result = eDao.query("select alarmId,alarmLevelName,firstStartTime,endTime,alarmDesc,alarmObjeDesc,carryCircuit from t_protection_channel_alarm ");
		return result;
	}
	public List<?> getAllSourceData(String date) {
		String sql = "select alarmId,alarmLevelName,firstStartTime,endTime,alarmDesc,alarmObjeDesc,carryCircuit from t_protection_channel_alarm"
				+ " where update_time=";
		List<?> result = eDao.query(sql + "'" + date + "'");
		return result;
	}
	public  void receiveAndSave(List<?> list) {
		ProtectEquipmentSave equipmentSave = new ProtectEquipmentSave(list);
		equipmentSave.saveToBean();
	}
	public class ProtectEquipmentSave{
		private List<?> list;
 
		public ProtectEquipmentSave(List<?> list) {
			super();
			this.list = list;
		}
		public void saveToBean(){
			List<ProtectChannelAlarm> equipments = new ArrayList<ProtectChannelAlarm>();
			for(Object obj : list){
				Object[] objs = (Object[]) obj;
				for(int i=0;i<objs.length;i++){
					if(objs[i]!=null&&"null".equals(objs[i].toString())){
						objs[i]=null;
					}
				}
				ProtectChannelAlarm findById = dao.findById(Long.parseLong(objs[0].toString()), ProtectChannelAlarm.class);
				if(findById!=null&&StringUtils.isNotBlank(String.valueOf(findById.getId()))){
					if(objs[1]!=null)findById.setLevel(objs[1].toString());
					if(objs[2]!=null)findById.setTriggerTime(DateUtil.getDateByString(objs[2].toString(), DateUtil.pattern_ymdhms));
					if(objs[3]!=null)findById.setRecoveryTime(DateUtil.getDateByString(objs[3].toString(), DateUtil.pattern_ymdhms));
					if(objs[4]!=null)findById.setName(objs[4].toString());
					if(objs[5]!=null)findById.setAlarmObject(objs[5].toString());
					if(objs[6]!=null)findById.setAlarmChannel(objs[6].toString());
				}else{
					ProtectChannelAlarm alarm=new ProtectChannelAlarm();
					alarm.setId(Long.parseLong(objs[0].toString()));
					if(objs[1]!=null)alarm.setLevel(objs[1].toString());
					if(objs[2]!=null)alarm.setTriggerTime(DateUtil.getDateByString(objs[2].toString(), DateUtil.pattern_ymdhms));
					if(objs[3]!=null)alarm.setRecoveryTime(DateUtil.getDateByString(objs[3].toString(), DateUtil.pattern_ymdhms));
					if(objs[4]!=null)alarm.setName(objs[4].toString());
					if(objs[5]!=null)alarm.setAlarmObject(objs[5].toString());
					if(objs[6]!=null)alarm.setAlarmChannel(objs[6].toString());
					equipments.add(alarm);
				}
			}
			try {
				dao.persistentBatch(ProtectChannelAlarm.class, equipments);
				log.info("更新保护业务根告警："+equipments.size());
				if(equipments!=null&&equipments.size()>0){
					long id1 = equipments.get(0).getId();
					long id2 = equipments.get(equipments.size()-1).getId();
					if(id1>id2){
						manager.generateUncompletedProtectCommunicationAlarm(String.valueOf(id2), String.valueOf(id1));
					}else{
						manager.generateUncompletedProtectCommunicationAlarm(String.valueOf(id1), String.valueOf(id2));
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
